import { defineConfig, loadEnv } from "vite";
import path from "path";
import createVitePlugins from "./vite/plugins";
import { fileURLToPath } from "url";

const filename = fileURLToPath(import.meta.url);
const _dirname = path.dirname(filename);

export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    build: {
      chunkSizeWarningLimit: 2000,
      outDir: "dist",
      minify: "terser",
      terserOptions: {
        compress: {
          keep_infinity: true,
          drop_console: true,
          drop_debugger: true
        },
        format: {
          comments: false
        }
      },
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]"
        }
      }
    },
    plugins: createVitePlugins(env, command === "build"),
    resolve: {
      alias: {
        "~": path.resolve(_dirname, "./"),
        "@": path.resolve(_dirname, "./src")
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"]
    },
    server: {
      port: 9090,
      host: true,
      open: false,
      hmr: true,
      proxy: {
        "/dev-api": {
          // target: "http://192.168.0.12:8080/",
          target: "https://test.51jzt.com/dev-api/",
          // target: "https://dev.51jzt.com/dev-api/",
          changeOrigin: true,
          rewrite: p => p.replace(/^\/dev-api/, "")
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/styles/mixin.scss";'
        }
      },
      postcss: {
        plugins: [
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: atRule => {
                if (atRule.name === "charset") {
                  atRule.remove();
                }
              }
            }
          }
        ]
      }
    }
  };
});
